package bscorp.appbase.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;

import java.sql.SQLException;
import java.util.List;

import bscorp.appbase.AppManager;
import bscorp.appbase.R;
import bscorp.appbase.utils.M4DatabaseUtil;
import bscorp.appbase.utils.M4Properties;

/**
 * Created by mengweima on 2017/8/21.
 */
public class M4DatabaseHelper extends OrmLiteSqliteOpenHelper {
    public static final String TAG = "M4DatabaseHelper.java";

    protected static M4DatabaseHelper instance;
    public M4DatabaseHelper.M4DataBaseListener databaseListener;

    public interface M4DataBaseListener {
        /**
         * 数据库升级时调用
         *
         * @param db
         * @param arg1
         * @param oldVersion
         * @param newVersion
         */
        public void databaseUpdate(SQLiteDatabase db, ConnectionSource arg1, int oldVersion, int newVersion);

        /**
         * 数据库新建时调用
         *
         * @return
         */
        public List<Class<?>> getCreatTableList();

    }

    public static M4DatabaseHelper getInstance(Context mContext, String databaseName, int databaseVersion) {
        mContext = mContext.getApplicationContext();
        if (instance == null) {
            synchronized (M4DatabaseHelper.class) {
                if (instance == null) {
                    instance = new M4DatabaseHelper(mContext, databaseName, databaseVersion);
                }
            }
        }
        return instance;
    }

    /**
     * 创建默认数据库，数据库名称：数据库版本从M4.propertis 获取
     *
     * @param mContext
     * @return
     */
    public static M4DatabaseHelper getInstance(Context mContext) {
        mContext = mContext.getApplicationContext();
        if (instance == null) {
            synchronized (M4DatabaseHelper.class) {
                if (instance == null) {
                    Log.d(TAG, "创建");
                    instance = new M4DatabaseHelper(mContext, M4Properties.getInstance().getConst("dbName"), new Integer(M4Properties.getInstance().getConst("dbVersion")));
                }
            }
        }
        return instance;
    }

    protected M4DatabaseHelper(Context context, String databaseName, int databaseVersion) {
        super(context, databaseName, null, databaseVersion, R.raw.ormlite_config);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            Log.d(TAG, "创建表");
            M4DatabaseUtil.createTables(getDatabaseListener().getCreatTableList(), connectionSource);
        } catch (SQLException e) {
            Log.e(TAG, "create tables error", e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource arg1, int oldVersion, int newVersion) {
        if (this.databaseListener != null) {
            databaseListener.databaseUpdate(db, arg1, oldVersion, newVersion);
        }
    }

    public static void clearHelper() {
        if (instance != null) {
            instance.close();
            instance = null;
        }
    }

    public void close() {
        super.close();
        instance = null;
    }

    public M4DataBaseListener getDatabaseListener() {
        return databaseListener;
    }

    public void setDatabaseListener(M4DataBaseListener databaseListener) {
        this.databaseListener = databaseListener;
    }
}
